package com.sf.webside.controller;

import com.sf.webside.bean.SysRquestBean;
import com.sf.webside.service.SysUserService;
import entity.ResultVO;
import entity.StatusCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.GsonUtils;
import util.ResultVOUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by tanjing on 2019/2/21.
 */
@RestController
@RequestMapping("/v1/sys")
@ResponseBody
public class SysController extends BaseExceptionHandler{
    private Logger logger= LoggerFactory.getLogger(SysController.class);

    /**
     * 登录
     * @param sysRquestBean
     * @return
     */
    @PostMapping(value = "/login")
    public Object login(@RequestBody SysRquestBean sysRquestBean) {
        if(null==sysRquestBean){
            return ResultVOUtil.error(StatusCode.ERROR,"参数不合法");
        }
        logger.info("login request "+ GsonUtils.getInstance().toJson(sysRquestBean));
        if(StringUtils.isBlank(sysRquestBean.getLoginName())){
            return ResultVOUtil.error(StatusCode.ERROR,"请输入用户名");
        }
        if(StringUtils.isBlank(sysRquestBean.getPassword())){
            return ResultVOUtil.error(StatusCode.ERROR,"请输入密码");
        }
        ResultVO resultVO=sysUserService.login(sysRquestBean);
        logger.info("login response "+ GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }

    /**
     * 获取字典
     * @param request
     * @param type
     * @return
     */
    @GetMapping(value = "/dict/{type}")
    public Object dict(HttpServletRequest request, @PathVariable String type) {
        ResultVO checkoutToken=checkoutToken(request);
        if(checkoutToken.getCode()!=200) {
            return checkoutToken;
        }
        logger.info("dict request "+ type);
        if(StringUtils.isBlank(type)){
            return ResultVOUtil.error(StatusCode.ERROR,"请输入类型");
        }
        ResultVO resultVO=sysUserService.dict(type);
        logger.info("dict response "+ GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
}
